package com.yhy;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;


public class HelloServerInitializer extends ChannelInitializer<SocketChannel>{
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        /**
         *  测试数据
         *  数据格式 [head 2 byte][length 2byte(只表示数据体)][data 数据不定长]
         *  ff fe 05 00 61 62 63 64 65
         */
        pipeline.addLast("framer", new LengthFrameDecoder(2048,2,
                2,0,4));

        //字符串编码和解码
        pipeline.addLast("decoder",new StringDecoder());
        pipeline.addLast("encoder",new StringEncoder());

        // 自己的逻辑Handler
        pipeline.addLast("handler",new HelloServerHandler());
    }
}